package cn.dcpnet.sample.pojo.base;

import java.util.Date;

/**
 * @author :   Dai Guoping
 * @version :   v1.0.0
 * @description result
 * @date :   Created in 2022/5/24 下午10:38
 */
public class ResponseResult<T> implements Cloneable {
  /**
     * response Code
     */
    private Integer code;
    /**
     * response Message
     */
    private String message;
    /**
     * response result
     */
    private T result;
    /**
     * response time
     */
    private Long time;

    private static final ResponseResult<?> SUCCESS_RESPONSE_DEFAULT = new ResponseResult<>() {{
        this.setCode(200);
        this.setMessage("success");
    }};

    private static final ResponseResult<?> ERROR_RESPONSE_DEFAULT = new ResponseResult<>() {{
        this.setCode(500);
        this.setMessage("error");
    }};

    public ResponseResult() {}

    public static ResponseResult OK() {
        ResponseResult clone = SUCCESS_RESPONSE_DEFAULT.clone();
        clone.setTime(System.currentTimeMillis());
        return clone;
    }

    public static <T> ResponseResult<T> OK(T result) {
        ResponseResult clone = SUCCESS_RESPONSE_DEFAULT.clone();
        clone.setTime(System.currentTimeMillis());
        clone.setResult(result);
        return clone;
    }

    public static ResponseResult ERROR() {
        ResponseResult clone = ERROR_RESPONSE_DEFAULT.clone();
        clone.setTime(System.currentTimeMillis());
        return clone;
    }

    public static <T> ResponseResult<T> ERROR(T result) {
        ResponseResult clone = ERROR_RESPONSE_DEFAULT.clone();
        clone.setTime(System.currentTimeMillis());
        clone.setResult(result);
        return clone;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    @Override
    protected ResponseResult<?> clone() {
        ResponseResult<?> result = null;
        try {
            result = (ResponseResult<?>) super.clone();
        } catch (Exception e) {
            throw new RuntimeException("Clone ResponseResult Failed");
        }
        return result;
    }
}
